package com.yaodian100.ec.member.web;

import java.text.ParseException;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.ec.member.entity.AddressBook;
import com.yaodian100.ec.member.service.MemberService;

public class AddressBookAction extends ActionSupport implements Preparable {

	private static final long serialVersionUID = -6769846069155060398L;
	/** logger */
	private final Logger logger = LoggerFactory.getLogger(AddressBookAction.class);
	private MemberService memberService;

	// 页面参数
	private AddressBook addressBook;

	private String id;
	private String memberId;
	private String contactName;
	private String zipCode;
	private String province;
	private String city;
	private String district;
	private String addr;
	private String mobile;
	private String areaCode;
	private String phone;
	private String extension;
	private String memo;
	private String saveResult = "";

	/**
	 * 新增地址簿资料
	 * 
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception {
		logger.info("memberId === :{}", memberId);
		setMemberId(memberId);
		addressBook = new AddressBook();
		id = "";
		return "edit";
	}

	/**
	 * 地址簿栏位验证
	 * 
	 * @return
	 * @throws ParseException
	 * @throws NumberFormatException
	 * @throws CoreException
	 */
	public boolean addressBookValidate() throws ParseException, NumberFormatException, CoreException {
		if (StringUtils.isBlank(memberId)) {
			setSaveResult("传入参数错误");
		} else {
			if (StringUtils.isNotBlank(id))
				addressBook = memberService.getAddressBook(Long.parseLong(id));
			else
				addressBook = new AddressBook();
			addressBook.setMemberId(memberId);
			/**************************** 必填栏位 ****************************/
			if (StringUtils.isBlank(contactName) || StringUtils.length(contactName) < 2
					|| StringUtils.length(contactName) > 16)
				setSaveResult("联络人姓名不得为空值且长度须介于2~16个字元之间");
			else
				addressBook.setContactName(contactName.trim());

			if (StringUtils.isBlank(zipCode))
				setSaveResult("请输入邮递区号");
			else
				addressBook.setZipCode(zipCode.trim());

			if (StringUtils.isBlank(province))
				setSaveResult("请选择省");
			else
				addressBook.setProvince(province);

			if (StringUtils.isBlank(city))
				setSaveResult("请选择城市");
			else
				addressBook.setCity(city);

			if (StringUtils.isBlank(district))
				setSaveResult("请选择街地区");
			else
				addressBook.setDistrict(district);

			if (StringUtils.isBlank(addr))
				setSaveResult("请输入地址");
			else
				addressBook.setAddr(addr.trim());

			/**************************** 非必填栏位 ****************************/
			if (StringUtils.isNotBlank(mobile))
				addressBook.setMobile(mobile.trim());

			if (StringUtils.isNotBlank(areaCode))
				addressBook.setAreaCode(areaCode.trim());

			if (StringUtils.isNotBlank(phone))
				addressBook.setPhone(phone.trim());

			if (StringUtils.isNotBlank(extension))
				addressBook.setExtension(extension.trim());

			if (StringUtils.isNotBlank(memo))
				addressBook.setMemo(memo.trim());
			logger.info("this.getFieldErrors() :{}", this.getFieldErrors());
		}
		// if (this.getFieldErrors().isEmpty() || this.getActionErrors().isEmpty())
		// return true;
		// else
		// return false;
		if (StringUtils.isNotBlank(getSaveResult()))
			return false;
		else
			return true;
	}

	public String delete() throws Exception {
		try {
			if (StringUtils.isNotBlank(memberId) && StringUtils.isNotBlank(id)) {
				memberService.deleteAddressBook(Long.parseLong(id));
			}
		} catch (CoreException e) {
			addActionError(e.getMessage());
		}
		return search();
	}

	/**
	 * 编辑地址簿資料
	 * 
	 * @return
	 */
	public String edit() throws Exception {
		if (StringUtils.isNotBlank(id))
			addressBook = memberService.getAddressBook(Long.parseLong(id));
		return "edit";
	}

	public String getAddr() {
		return addr;
	}

	public AddressBook getAddressBook() {
		return addressBook;
	}

	public String getAreaCode() {
		return areaCode;
	}

	/*
	 * ***************************************************
	 * 页面参数 ***************************************************
	 */

	public String getCity() {
		return city;
	}

	public String getContactName() {
		return contactName;
	}

	public String getDistrict() {
		return district;
	}

	public String getExtension() {
		return extension;
	}

	public String getId() {
		return id;
	}

	public String getMemberId() {
		return memberId;
	}

	public String getMemo() {
		return memo;
	}

	public String getMobile() {
		return mobile;
	}

	public String getPhone() {
		return phone;
	}

	public String getProvince() {
		return province;
	}

	public String getSaveResult() {
		return saveResult;
	}

	public String getZipCode() {
		return zipCode;
	}

	public void prepare() throws Exception {
		clearErrorsAndMessages();
		logger.info("prepare ok");
	}

	/**
	 * 新增/修改地址簿资料
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		try {
			if (addressBookValidate()) {
				boolean result = (memberService.checkAddressBookExist(addressBook));
				if (StringUtils.isNotBlank(id)) {
					if (!result)
						addressBook = memberService.updateAddressBook(addressBook);
					else
						setSaveResult("修改失败,地址簿资料重复");
				} else {
					if (!result)
						addressBook = memberService.saveAddressBook(addressBook);
					else
						setSaveResult("新增失败,地址簿资料重复");
				}
			}
		} catch (Exception e) {
			setSaveResult(e.getMessage());
		}
		return "jsonSaveAdBook";
	}

	/**
	 * 地址簿资料列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String search() throws Exception {
		return "list";
	}

	public void setAddr(String addr) {
		this.addr = addr;
	}

	public void setAddressBook(AddressBook addressBook) {
		this.addressBook = addressBook;
	}

	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public void setContactName(String contactName) {
		this.contactName = contactName;
	}

	public void setDistrict(String district) {
		this.district = district;
	}

	public void setExtension(String extension) {
		this.extension = extension;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setMemberId(String memberId) {
		this.memberId = memberId;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public void setSaveResult(String saveResult) {
		this.saveResult = saveResult;
	}

	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}

}